/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linkeddata.arida.ufc.br.VCD.project;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 *
 * @author jeovane
 */
public class CreateVCDProject {

    private Document document;
    private String name;
    private String type;
    private String[] parameterListSelect;
    private String[] parameterListCondition;
    private int limit;
    private String nameDestination;
    private String[] destinationParameter;
    private String[] query;
    private Element VCD = new Element("VCD");
    private Element ParameterList = new Element("ParameterList");
    private Element ParameterListSelect = new Element("Select");
    private Element Attribute;
    private Element Variables = new Element("Variables");
    private String variable = "";
    private Element ParameterListSortCondition = new Element("Sort-condition");
    private Element ParameterListLimit = new Element("Limit");
    private Element Elos = new Element("Elos");
    private Element ElosParameters = new Element("Parameters");
    private Element ElosDestination = new Element("Destination");
    private Element Parameter;
    private Element SparqlQuery = new Element("SPARQLQuery");
    private Element SparqlQueryOperator;
    private Element SparqlQueryQuery;
    private static final String ATTRIBUTE_NAME = "name";
    private static final String ATTRIBUTE_TYPE = "type";
    private static final String ATTRIBUTE_OPERATOR = "operator";
    private static final String ATTRIBUTE_VCD = "vcd";
    private static final String ATTRIBUTE_ID = "id";
    private static final String ATTRIBUTE_SERVICE_URI = "serviceURI";

    public CreateVCDProject(String name, String type, String[] parameterListSelect, String[] parameterListCondition, int limit, String nameDestination, String[] destinationParameter, String[] query) {
        this.name = name;
        this.type = type;
        this.parameterListSelect = parameterListSelect;
        this.parameterListCondition = parameterListCondition;
        this.limit = limit;
        this.nameDestination = nameDestination;
        this.destinationParameter = destinationParameter;
        this.query = query;
    }

    public String createVcdXML() throws Exception {
        document = new Document(VCD);
        VCD.setAttribute(ATTRIBUTE_NAME, name).setAttribute(ATTRIBUTE_TYPE, type);

        for (int i = 0; i < parameterListSelect.length; i++) {
            if (i < parameterListSelect.length - 1) {
                variable += parameterListSelect[i] + ", ";
            } else {
                variable += parameterListSelect[i];
            }
            Variables.setText(variable);
        }

        for (int i = 0; i < parameterListCondition.length; i++) {
            String[] conditionXML = parameterListCondition[i].split(" [?]:");
            Attribute = new Element("Attribute");
            Attribute.setAttribute(ATTRIBUTE_OPERATOR, conditionXML[0]).setText(conditionXML[1]);
            ParameterListSortCondition.addContent(Attribute);
        }

        document.getRootElement().addContent(ParameterList.
                addContent(ParameterListSelect.addContent(Variables)).
                addContent(ParameterListSortCondition).
                addContent(ParameterListLimit.setText(Integer.toString(limit))));

        for (int i = 0; i < destinationParameter.length; i++) {
            Parameter = new Element("Parameter");
            Parameter.setText(destinationParameter[i]);
            ElosDestination.addContent(Parameter);
        }

        document.getRootElement().addContent(Elos.
                addContent(ElosParameters.
                addContent(ElosDestination.
                setAttribute(new Attribute(ATTRIBUTE_VCD, nameDestination)))));

        for (int i = 0; i < query.length; i++) {
            String[] queryXML = query[i].split(" [?]:");
            SparqlQueryQuery = new Element("Query");
            SparqlQueryOperator = new Element("Operator");
            SparqlQueryQuery.setAttribute(ATTRIBUTE_SERVICE_URI, queryXML[1]).setText(queryXML[2]);
            SparqlQueryOperator.setAttribute(new Attribute(ATTRIBUTE_ID, queryXML[0]))
                    .addContent(SparqlQueryQuery);
            SparqlQuery.addContent(SparqlQueryOperator);
        }
        document.getRootElement().addContent(SparqlQuery);

        return printDocument(document);
    }

    private String printDocument(Document document) throws Exception {
        return new XMLOutputter(Format.getPrettyFormat()).outputString(document);
    }

    public static void main(String[] args) throws Exception {
        String name = "diseases-name";
        String type = "Initial";
        String[] parameterListSelect = {"Disease_name", "Drug_name"};
        String[] parameterListCondition = {"ORDER BY ?:Drug_name"};
        int limit = 30;
        String nameDestination = "drugs-name";
        String[] destinationParameter = {"Disease_name"};
        String[] query = {"1 ?:http://localhost:3030/diseasome/sparql ?:PREFIX diseasome: <http://www4.wiwiss.fu-berlin.de/diseasome/resource/diseasome/>\n"
            + "SELECT *\n"
            + "WHERE {\n"
            + "    ?ds diseasome:name ?Disease_name.\n"
            + "}"};

        CreateVCDProject project = new CreateVCDProject(name, type, parameterListSelect, parameterListCondition, limit, nameDestination, destinationParameter, query);

        System.out.println(project.createVcdXML());
    }
}
